﻿using System;
using System.Data;
using System.Text;
using System.Web.UI;
using BO.Infrastructure.StaticData;
using BO.Infrastructure.Utils;
using BugManager;

namespace GUI.Controls.Video
{
    public partial class HighlightVideo : System.Web.UI.UserControl
    {
        private const string LiFormat = "<li rel=\"{3}\"><a class=\"link-img\" href=\"{0}\"><img id=\"imgplaylist\" src=\"{2}\" /></a><a class=\"lnk\" href=\"{0}\">{1}</a></li>";
        protected string ShareButtonFormat = "<a class=\"sharefacebook\" href=\"http://www.facebook.com/sharer.php?u={0}\" target=\"_blank\"></a>";
        protected string LikeButtonFormat = "<iframe src=\"http://www.facebook.com/plugins/like.php?href={0}&amp;layout=standard&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;send=true\" scrolling=\"no\" frameborder=\"0\" allowtransparency=\"true\"></iframe>";
        private const int VideoWidth = 606;
        private const int VideoHeight = 357;


        protected void Page_Load(object sender, EventArgs e)
        {


        }

        public bool DataBind(DataTable highlightVideo, DataTable newestVideo)
        {
            try
            {
                #region Video nổi bật
                {
                    DataRow r = null;
                    if (highlightVideo != null && highlightVideo.Rows.Count > 0)
                    {
                        r = highlightVideo.Rows[0];
                    }
                    else
                    {
                        if (newestVideo != null && newestVideo.Rows.Count > 0)
                            r = newestVideo.Rows[0];
                    }
                    if (r != null)
                    {
                        ltrEmbedCode.Text = PresentationUtils.Video.BuildEmbedCode(
                            r[SqlConstants.Video.KeyVideo].ToString(),
                            r[SqlConstants.Video.PName].ToString(),
                            VideoWidth,
                            VideoHeight,
                            PresentationUtils.Photo.GetVideoAvatarSource(r[SqlConstants.Video.Avatar].ToString()));
                        ltrTitle.Text = r[SqlConstants.Video.Name].ToString();
                        ltrDesc.Text = r[SqlConstants.Video.Description].ToString();
                        ltrTags.Text = r[SqlConstants.Video.Tags].ToString();
                        var absoluteUrl = PresentationUtils.UrlBuilder.BuildAbsoluteUrl(r[SqlConstants.Video.Url].ToString());
                        ltrShareButton.Text = String.Format(ShareButtonFormat, absoluteUrl);
                        ltrLikeButton.Text = String.Format(LikeButtonFormat, absoluteUrl);
                    }
                }
                #endregion

                if (newestVideo != null && newestVideo.Rows.Count > 0)
                {
                    var sb = new StringBuilder();
                    foreach (DataRow r in newestVideo.Rows)
                    {
                        sb.AppendFormat(LiFormat,
                                        r[SqlConstants.Video.Url],
                                        r[SqlConstants.Video.Name],
                                        r[SqlConstants.Video.Avatar],
                                        r[SqlConstants.Video.Id]);
                    }

                    ltrListCont.Text = sb.ToString();

                    var jsonData = PresentationUtils.ObjectToJson(newestVideo);
                    var script = string.Format("sohavideo.setCurrentData({0});", jsonData);
                    Page.ClientScript.RegisterClientScriptBlock(typeof(string), string.Empty, script, true);
                }
                return true;
            }
            catch (Exception ex)
            {
                ErrorManager.Log("HighlightVideo.ascx", "DataBind", ex.Message);
            }
            return false;
        }
    }
}